<?php

/**
 * @author lzp300
 * @copyright 2015-03-26
 */

class index extends actionAbstract{
    /**
     * 构造函数
     * @var array
     */
    function __construct() {
        parent::__construct();
	    set_time_limit(0);
    }
    
    /**
     * 权限MD5加密
     * 测试：luo2b   luo2b   正式：key=>uni1001    id=>unibooking
     */
    function SignUtil($serviceName="GetAllOtaPartnerInfo",$accountKey="luo2b",$version="1.1.0",$accountID="luo2b"){
        $reqTime=date('Y-m-d',time());
        
        $resuoce=array();
        $resuoce['state']=0;
        $resuoce['string']="";
        
        $originalArray=array();
        $originalArray['Version']=$version;
        $originalArray['AccountID']=$accountID;
        $originalArray['ReqTime']=$reqTime;
        if($serviceName){
            $originalArray['ServiceName']=$serviceName;
            ksort($originalArray);
            $sortedArray="";
            foreach($originalArray as $k=>$v){
                $sortedArray.=$k.'='.$v.'&';
            }
            $sortedArray=trim($sortedArray,'&');
            $keywords=md5($sortedArray.$accountKey);
            $resuoce['state']=1;
            $resuoce['string']['version']=$version;
            $resuoce['string']['accountId']=$accountID;
            $resuoce['string']['serviceName']=$serviceName;
            $resuoce['string']['sign']=$keywords;
            $resuoce['string']['reqTime']=$reqTime;
            //var_dump($resuoce);
        }else{
            $resuoce['string']='接口方法名错误！';
        }
        return $resuoce;
    }
    
    /**
     * curl 传输      //测试：120.24.75.164:8999---正式：120.24.166.230:8999
     */
    function curl($post_data,$url="http://120.24.75.164:8999/PmsInterfaceConnections/PmsDataInterFace/getPmsMethod"){
        //print_r($post_data);exit;
        $ch = curl_init();
        //print_r($ch);
        curl_setopt ( $ch, CURLOPT_URL, $url );
        curl_setopt ( $ch, CURLOPT_POST, 1 );
        curl_setopt ( $ch, CURLOPT_HEADER, 0 );
        curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $ch, CURLOPT_HTTPHEADER, array("Content-Type: application/json; charset=utf-8"));
        curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
        $return = curl_exec ( $ch );
        curl_close ( $ch );
        $return=json_decode($return);
        $body=array();
        $head=array();
        if($return){
            $body=(array)$return->body;
            $head=(array)$return->head;
            foreach($body as $k=>$v){
                if(is_object($v)){
                    $body[$k]=(array)$v;
                }
            }
        //var_dump($head);exit;
        }else{
            echo "获取内容失败！";
        }
        return $body;
        //echo "<pre>";print_r($head);exit;
    }
    
    
    /**
     * 获取省
     */
    function GetOsii_Province(){
        $resuoce=$this->SignUtil('GetOsii_Province');
        if($resuoce['state']==0){
            echo $resuoce['string'];
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            //echo "<pre>";print_r($post_data);exit;
            $contArray=$this->curl($post_data);
            //echo "<pre>";print_r($contArray);exit;
            $this->loadModel('tc','province');
            if($contArray){
                foreach($contArray as $k=>$v){
                    $data['name']=$v['name'];
                    $data['en_name']=$v['en_name'];
                    $data['prefixLetter']=$v['prefix_letter'];
                    $re=$this->tc->provinceModel->fetchRow("select * from tc_province where osiiid=".$v['id']);
                    if($re){
                        $retu=$this->tc->provinceModel->update($data,$re['id']);
                    }else{
                        $data['osiiid']=$v['id'];
                        $retu=$this->tc->provinceModel->insert($data);
                    }
                }
            }
            if($retu){
                echo "OK";
            }else{
                echo "NO";
            }
            echo '<pre>';print_r($contArray);
        }
    }
    
    /**
     * 获取市
     */
    function GetOsii_City(){
        $resuoce=$this->SignUtil('GetOsii_City');
        if($resuoce['state']==0){
            echo $resuoce['string'];
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            $contArray=$this->curl($post_data);
            
            $this->loadModel('tc','city');
            if($contArray){
                foreach($contArray as $k=>$v){
                    sleep(2);
                    $data['name']=$v['name'];
                    $data['en_name']=$v['en_name'];
                    $data['prefix_letter']=$v['prefix_letter'];
                    $data['province_id']=$v['province_id'];
                    $re=$this->tc->cityModel->fetchRow("select * from tc_city where osiiid=".$v['id']);
                    if($re){
                        $retu=$this->tc->cityModel->update($data,$re['id']);
                    }else{
                        $data['osiiid']=$v['id'];
                        $retu=$this->tc->cityModel->insert($data);
                    }
                    ob_flush();flush();
                }
            }
            if($retu){
                echo "OK";
            }else{
                echo "NO";
            }
            //echo '<pre>';print_r($contArray);
        }
    }
    
    /**
     * 获取区
     */
    function GetOsii_County(){
        $resuoce=$this->SignUtil('GetOsii_County');
        if($resuoce['state']==0){
            echo $resuoce['string'];
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            $contArray=$this->curl($post_data);
            
            $this->loadModel('tc','county');
            if($contArray){
                foreach($contArray as $k=>$v){
                    sleep(2);
                    $data['name']=$v['name'];
                    $data['en_name']=$v['en_name'];
                    $data['prefix_letter']=$v['prefix_letter'];
                    $data['city_id']=$v['city_id'];
                    $re=$this->tc->countyModel->fetchRow("select * from tc_city where osiiid=".$v['id']);
                    if($re){
                        $retu=$this->tc->countyModel->update($data,$re['id']);
                    }else{
                        $data['osiiid']=$v['id'];
                        $retu=$this->tc->countyModel->insert($data);
                    }
                    ob_flush();flush();
                }
            }
            if($retu){
                echo "OK";
            }else{
                echo "NO";
            }
            //echo '<pre>';print_r($contArray);
        }
    }
    
    
    //获取渠道
    function GetOsii_channels(){
        $resuoce=$this->SignUtil('GetAllOtaPartnerInfo','osii_interface_2004_!@#','1.1.0','osii_interface_2004');
        if($resuoce['state']==0){
            echo $resuoce['string'];
        }elseif($resuoce['state']==1){
            $post_data=json_encode(array("head"=>$resuoce['string'],"body"=>array()));
            $contArray=$this->curl($post_data,"http://www.wormholehotels.com/OsiiInterFace/getMethod.do");
            //echo "<pre>";print_r($contArray);exit;
            $this->loadModel('pms','channels');
            if($contArray){
                foreach($contArray as $k=>$v){
                    $data['osiiId']=$v['otaCode'];
                    $data['osiiType']=$v['otaType'];
                    $data['title']=$v['otaName'];
                    $re=$this->pms->channelsModel->fetchRow("select * from pms_channels where osiiId=".$v['otaCode']);
                    if($re){
                        $retu=$this->pms->channelsModel->update($data,$re['id']);
                    }else{
                        $data['osiiId']=$v['otaCode'];
                        $retu=$this->pms->channelsModel->insert($data);
                    }
                }
            }
            if($retu){
                return 1;
            }else{
                return 0;
            }
        }
    }
    
    
    
}